package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Adapters.AddStuListAdapter;
import com.example.starseas_phone.Entity.AddStuList;
import com.example.starseas_phone.Entity.Apply;
import com.example.starseas_phone.Entity.Students;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddStuList_ApplyActivity extends AppCompatActivity {

    private String TAG = "AddStuList_ApplyActivity测试";

    private TextView add_stu_list_apply_t1;
    private TextView add_stu_list_apply_t2;
    private TextView add_stu_list_apply_t3;
    private TextView add_stu_list_apply_t4;

    private List<Students> list = new ArrayList<>();
    private ListView add_stu_list_apply_list;
    private AddStuListAdapter adapter;

    private Button add_stu_list_apply_btn1;
    private Button add_stu_list_apply_btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_stu_list_apply);
        Intent intent = getIntent();
        Apply apply = (Apply) intent.getSerializableExtra("apply");
        initid();
        initview(apply);
        add_stu_list_apply_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Students students = adapter.getItem(i);
                Intent intent = new Intent(AddStuList_ApplyActivity.this,AddStu_detailsActivity.class);
                intent.putExtra("students",students);
                intent.putExtra("str","添加学生详细信息");
                startActivity(intent);
            }
        });

        add_stu_list_apply_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ty(apply,1);
            }
        });

        add_stu_list_apply_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ty(apply,2);
            }
        });
    }

    private void initid(){
        add_stu_list_apply_t1 = (TextView) findViewById(R.id.add_stu_list_apply_t1);
        add_stu_list_apply_t2 = (TextView) findViewById(R.id.add_stu_list_apply_t2);
        add_stu_list_apply_t3 = (TextView) findViewById(R.id.add_stu_list_apply_t3);
        add_stu_list_apply_t4 = (TextView) findViewById(R.id.add_stu_list_apply_t4);

        add_stu_list_apply_list = (ListView) findViewById(R.id.add_stu_list_apply_list);
        adapter = new AddStuListAdapter(AddStuList_ApplyActivity.this,R.layout.add_stu_list_listview,list);

        add_stu_list_apply_btn1 = (Button) findViewById(R.id.add_stu_list_apply_btn1);
        add_stu_list_apply_btn2 = (Button) findViewById(R.id.add_stu_list_apply_btn2);
    }

    private void initview(Apply apply){
        try {
            add_stu_list_apply_t1.setText(apply.getApplicantsId());
            add_stu_list_apply_t2.setText(apply.getByTheTime());
            add_stu_list_apply_t3.setText("添加学生信息");
            String a = "";
            if (apply.getStatus()==2){
                a = "教师已同意,教务处未审批";
            }else if (apply.getStatus()==4){
                a = "教师已同意,教务处已同意";
            }else {
                a = "教师已同意,教务处未同意";
            }
            add_stu_list_apply_t4.setText(a);
            JSONArray jsonArray = new JSONArray(apply.getInfoJson());
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String StuName = jsonObject.getString("StuName");
                int StuAge = jsonObject.getInt("StuAge");
                String StuSex = jsonObject.getString("StuSex");
                String StuPhone = jsonObject.getString("StuPhone");
                String StuEmail = jsonObject.getString("StuEmail");
                String StuCarid = jsonObject.getString("StuCarid");
                String StuClassId = jsonObject.getString("ClassId");
                String StubackTime = jsonObject.getString("StubackTime");
                String PoliticalLandscape = jsonObject.getString("PoliticalLandscape");
                String NativePlace = jsonObject.getString("NativePlace");
                String National = jsonObject.getString("National");
                String StateOfTheSchool = jsonObject.getString("StateOfTheSchool");
                Students students = new Students(StuName,StuAge,StuSex,StuPhone,StuEmail,StuCarid,StuClassId,StubackTime,PoliticalLandscape,NativePlace,National,StateOfTheSchool);
                list.add(students);
            }
            adapter.notifyDataSetChanged();
            add_stu_list_apply_list.setAdapter(adapter);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void ty(Apply apply,int status){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("Id",apply.getId());
                    jsonObject1.put("ApplicantsId",apply.getApplicantsId());
                    jsonObject1.put("InfoJson",apply.getInfoJson());
                    jsonObject1.put("ByTheTime",apply.getByTheTime());
                    jsonObject1.put("ApplicationType",apply.getApplicationType());
                    jsonObject1.put("Status",apply.getStatus());

                    MediaType mediaType = MediaType.parse("application/json-patch+json");
                    RequestBody requestBody = RequestBody.create(mediaType,jsonObject1.toString()+"");
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/TheApplicationForm/AlterStuDeptApproval?token="+Sto.dept.getToken()+"&status="+status)
                            .put(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    Log.d(TAG+"msg",msg);
                    JSONArray jsonArray = new JSONArray(msg);
                    if (jsonArray.length()==1){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(AddStuList_ApplyActivity.this,"拒绝成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
                    }else {
                        JSONObject jsonObject2 = jsonArray.getJSONObject(0);
                        String Id1 = jsonObject2.getString("Id");
                        String message1 = jsonObject2.getString("message");
                        JSONObject jsonObject22 = new JSONObject(message1);
                        int message11 = jsonObject22.getInt("message");
                        JSONObject jsonObject3 = jsonArray.getJSONObject(1);
                        String Id2 = jsonObject3.getString("Id");
                        String message2 = jsonObject3.getString("message");
                        JSONArray jsonArray1 = new JSONArray(message2);
                        JSONObject jsonObject33 = jsonArray1.getJSONObject(0);
                        String message22 = jsonObject33.getString("message");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String result = "";
                                if (message11>0){
                                    result += Id1+"成功，";
                                }else {
                                    result += Id1+"失败，";
                                }
                                if (message22.equals("error")){
                                    result = result+Id2+"失败";
                                }else if (message22.equals("success")){
                                    result = result+Id2+"成功";
                                }else {
                                    result = result+Id2+message22;
                                }
                                Toast.makeText(AddStuList_ApplyActivity.this,result,Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}